import ajaxMainTableData from './ajaxMainTableData';
import updateButtonStatus from "./updateButtonStatus";
import {getQueryCondition} from "src/hrpub/common/utils/utils";

export default function getMainTableData() {
    const {
        searchModalValue,
        orgVal,
        queryOid,
        pageInfo,
        sort_field,
        rosterRefVal
    } = this.state;
    let data = getQueryCondition(this.props, 'query');
    if (!orgVal.refpk || !rosterRefVal.refpk) return;

    let postData = {
        ...data,
        pk_org: this.props.isOrg ? orgVal.refpk : '',//组织传值和pk_org_ref一样
        pk_org_ref: orgVal.refpk,
        pageInfo,
        //oid: queryOid,
        pk_rpt_def: rosterRefVal.refpk,
        sort_field: JSON.stringify(sort_field)
    };

    return ajaxMainTableData(postData)
        .then((res) => {
            if (res.success) {
                let tableData = res.data.data.map(item => {
                    let rowData = {
                        status: "0",
                        isOptimized: false,
                        values: {}
                    };
                    Object.keys(item).forEach(key => {
                        rowData.values[key] = {
                            display: item[key],
                            value: item[key]
                        }
                    });
                    return rowData;
                });
                //res.data.data.forEach((item, index) => item.numberindex = index + 1);
                this.props.table.setAllTableData('rosterTable', {
                    rows: tableData
                });
                this.setState({
                    hasSearched: true,
                    //selectedRowIndex: res.data.data && res.data.data.length ? 0 : '',
                    //rosterData: res.data.data,
                    pageInfo: res.data.pageInfo || {
                        ...pageInfo,
                        pageIndex: 1,
                        total: 0,
                        totalPage: 1
                    },
                }, () => {
                    updateButtonStatus.call(this);
                });
            }
        });
}
